﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using W7Toshil.Entity;
using W7Toshil.Services;
using W7Toshil.Util;

namespace W7Toshil
{
    public partial class MainPage : PhoneApplicationPage
    {
        // Constructor
        public MainPage()
        {
            this.DataContext = this;
            InitializeComponent();
        }

        private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
        {
            ITransaction<Expense> expenseService = new ExpenseTransaction();
            List<Expense> expenseTransactions = expenseService.FetchAll() as List<Expense>;
            if (null == expenseTransactions || expenseTransactions.Count == 0)
            {
                this.NavigationService.Navigate(new Uri("/AddExpenses.xaml", UriKind.Relative));
            }
            else
            {

            }
            {
                PopulateTransaction(Constants.Summary_TagName , expenseTransactions);


            }

            //this.fileList.ItemsSource = GetFileNames();
        }

        //private System.Collections.IEnumerable GetFileNames()
        //{
        //    List<TestFile> fileList = new List<TestFile>();
        //    TestFile file = new TestFile()
        //    {
        //         Name = "File1",
        //         Content = "File 1 Content"
        //    };

        //    fileList.Add(file);

        //    file = new TestFile()
        //    {
        //        Name = "File2",
        //        Content = "File 2 Content"
        //    };
        //    fileList.Add(file);

        //    file = new TestFile()
        //    {
        //        Name = "File3",
        //        Content = "File 3 Content"
        //    };
        //    fileList.Add(file);
        //    //fileList[0] = "File1";
        //    //fileList[1] = "File2";
        //    //fileList[2] = "File3";

        //    BindTest test = new BindTest()
        //    {
        //        Header = "Files",
        //        FileList = fileList
        //    };

        //    List<BindTest> bindTestList = new List<BindTest>();
        //    bindTestList.Add(test);
        //    return bindTestList;
        //}

        private void PopulateTransaction(string category, List<Expense> expenseTransactions)
        {
            
            ITransactionViewBuilder builder = null;
            if (category == Constants.Summary_TagName )
            {
                builder = new TagNameTransactionViewBuilder();
            }
            else if(category == Constants.Summary_Timeline )
            {
                builder = new TimelineTransactionViewBuilder();
            }
            
            foreach (Expense expense in expenseTransactions)
            {
                builder.Add(expense);

            }

            List<TransactionView> transactionViewList = builder.FetchAll() as List<TransactionView>;

            transactionListBox.ItemsSource = transactionViewList;
            //TransactionDetails.ItemsSource = this.TransactionList;

        }

        private void AddButton_Click(object sender, EventArgs e)
        {
            this.NavigationService.Navigate(new Uri("/AddExpenses.xaml",UriKind.Relative ));
        }

        private void TagNameButton_Click(object sender, RoutedEventArgs e)
        {
            ITransaction<Expense> expenseService = new ExpenseTransaction();
            List<Expense> expenseTransactions = expenseService.FetchAll() as List<Expense>;
            PopulateTransaction(Constants.Summary_TagName, expenseTransactions);
        }

        private void TimelineButton_Click(object sender, RoutedEventArgs e)
        {
            ITransaction<Expense> expenseService = new ExpenseTransaction();
            List<Expense> expenseTransactions = expenseService.FetchAll() as List<Expense>;
            PopulateTransaction(Constants.Summary_Timeline, expenseTransactions);

        }

        

       


    }
}